package cn.com.pajx.pajx_visitor_v3.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;
import java.security.MessageDigest;

import cn.com.pajx.pajx_visitor_v3.R;

/**
 * 图片加载类
 */
public class BaseImageUtils {

    public static void setRoundImage(final Context context, final String url, ImageView imageView, int drawableId, int radius) {
        Glide.with(context)
                .load(url)
                .transform(new CircleCornerForm(context, radius))
                .placeholder(drawableId)
                .error(drawableId)
                .into(imageView);
    }

    public static void setLocalCornerImage(final Context context, ImageView imageView, int drawableId, CornerTransform transform) {
        Glide.with(context)
                .load(drawableId)
                .transform(transform)
                .placeholder(drawableId)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(drawableId)
                .into(imageView);
    }

    public static void setFileCornerImage(Context context, ImageView imageView, File file, CornerTransform transform) {
        Glide.with(context)
                .load(file)
                .transform(transform)
                .placeholder(R.mipmap.icon_avatar)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(R.mipmap.icon_avatar)
                .into(imageView);
    }

    public static void setNetCornerImage(Context context, ImageView imageView, String url, int default_icon, CornerTransform transform) {
        Glide.with(context)
                .load(url)
                .transform(transform)
                .placeholder(default_icon)
                .skipMemoryCache(true)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(default_icon)
                .into(imageView);
    }

    public static void setImage(final Context context, final String url, ImageView imageView, RequestListener<Drawable> callback) {
        setImage(context, url, 0, 0, 0, imageView, callback);
    }

    public static void setImageNoCache(Context context, String url, int default_icon, ImageView imageView) {
        Glide.with(context).load(url)
                .placeholder(default_icon)
                .error(default_icon)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(imageView);
    }

    public static void setVideoThumbnail(Context context, String url, int default_icon, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions().frame(1000000)//单位微秒
                .centerCrop()
                .error(R.mipmap.default_logo)
                .placeholder(R.mipmap.default_logo);
        Glide.with(context)
                .setDefaultRequestOptions(requestOptions)
                .load(url)
                .into(imageView);
    }

    public static void setImage(Context context, String url, ImageView imageView) {
        setImage(context, url, imageView, 0);
    }

    public static void setImage(Context context, String url, ImageView imageView, int defaultIcon) {
        setImage(context, url, 0, defaultIcon, imageView);
    }

    public static void setImage(Context context, String url, int size, int defaultIcon, ImageView imageView) {
        setImage(context, url, size, size, defaultIcon, imageView);
    }

    public static void setImage(Context context, String url, int width, int height, int defaultIcon, ImageView imageView) {
        setImage(context, url, width, height, defaultIcon, imageView, null);
    }

    /***
     * file
     */
    public static void setImage(Context context, File file, ImageView imageView, int defaultIcon) {
        setImage(context, file, 0, 0, defaultIcon, imageView, null);
    }

    public static void setImage(Context context, File file, int size, int defaultIcon, ImageView imageView) {
        setImage(context, file, size, size, defaultIcon, imageView, null);
    }

    ////////////////base//////////////////////
    @SuppressLint("CheckResult")
    public static void setImage(final Context context, final String url, int width, int height, int defaultIcon, ImageView imageView, RequestListener<Drawable> callback) {

        if (defaultIcon == 0) {
            defaultIcon = R.mipmap.default_logo;
        }
        RequestManager requestManager = Glide.with(context);
        //
        RequestBuilder requestBuilder;
        if (!TextUtils.isEmpty(url) && !TextUtils.isEmpty(url.trim())) {
            requestBuilder = requestManager.load(url.trim()).listener(callback);
        } else {
            requestBuilder = requestManager
                    .load(defaultIcon).listener(callback);
        }
        if (width != 0 && height != 0) {
            requestBuilder.override(width, height).centerCrop();
        }

        requestBuilder.error(defaultIcon)
                .placeholder(defaultIcon);

        requestBuilder.into(imageView);

    }

    @SuppressLint("CheckResult")
    public static void setImage(final Context context, final File file,
                                int width, int height, int defaultIcon,
                                ImageView imageView, RequestListener<Drawable> callback) {
        if (defaultIcon == 0) {
            defaultIcon = R.mipmap.default_logo;
        }
        RequestManager requestManager = Glide.with(context);
        //
        RequestBuilder requestBuilder;
        if (file != null) {
            requestBuilder = requestManager
                    .load(file).listener(callback);
        } else {
            requestBuilder = requestManager
                    .load(defaultIcon);
        }
        if (width != 0 && height != 0) {
            requestBuilder.override(width, height);
        }
        requestBuilder.error(defaultIcon)
                .placeholder(defaultIcon)
                .centerCrop();
        requestBuilder.into(imageView);

    }

    public static class CircleCornerForm implements Transformation<Bitmap> {
        private final BitmapPool mBitmapPool;

        private float radius;

        CircleCornerForm(Context context, float radius) {
            this.mBitmapPool = Glide.get(context).getBitmapPool();
            this.radius = radius;
        }


        @NonNull
        @Override
        public Resource<Bitmap> transform(@NonNull Context context, @NonNull Resource<Bitmap> resource, int outWidth, int outHeight) {
            Bitmap source = resource.get();
            int finalWidth, finalHeight;
            float ratio; //输出目标的宽高或高宽比例
            if (outWidth > outHeight) { //输出宽度>输出高度,求高宽比
                ratio = (float) outHeight / (float) outWidth;
                finalWidth = source.getWidth();
                finalHeight = (int) ((float) source.getWidth() * ratio); //固定原图宽度,求最终高度
                if (finalHeight > source.getHeight()) { //求出的最终高度>原图高度,求宽高比
                    ratio = (float) outWidth / (float) outHeight;
                    finalHeight = source.getHeight();
                    finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
                }
            } else if (outWidth < outHeight) { //输出宽度 < 输出高度,求宽高比
                ratio = (float) outWidth / (float) outHeight;
                finalHeight = source.getHeight();
                finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
                if (finalWidth > source.getWidth()) { //求出的最终宽度 > 原图宽度,求高宽比
                    ratio = (float) outHeight / (float) outWidth;
                    finalWidth = source.getWidth();
                    finalHeight = (int) ((float) source.getWidth() * ratio);
                }
            } else { //输出宽度=输出高度
                finalHeight = source.getHeight();
                finalWidth = finalHeight;
            }

            //修正圆角
            this.radius *= (float) finalHeight / (float) outHeight;
            Bitmap outBitmap = this.mBitmapPool.get(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(outBitmap);
            Paint paint = new Paint();
            //关联画笔绘制的原图bitmap
            BitmapShader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            //计算中心位置,进行偏移
            int width = (source.getWidth() - finalWidth) / 2;
            int height = (source.getHeight() - finalHeight) / 2;
            if (width != 0 || height != 0) {
                Matrix matrix = new Matrix();
                matrix.setTranslate((float) (-width), (float) (-height));
                shader.setLocalMatrix(matrix);
            }

            paint.setShader(shader);
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight());
            canvas.drawRoundRect(rectF, this.radius, this.radius, paint); //先绘制圆角矩形

            return BitmapResource.obtain(outBitmap, this.mBitmapPool);
        }

        @Override
        public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

        }
    }
}
